﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using 软件工程实验.Models;

namespace 软件工程实验.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Transcripts()
        {
            ViewBag.Message = "以下是所有学生的成绩";
            var model = DBHelper.DB.成绩表.OrderByDescending(o => o.总成绩).ToList();

            return View(model);
        }
        [HttpGet]
        public ActionResult Login()
        {
            ViewBag.Message = "请输入密码";
            return View();
        }
        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                // 验证用户凭据
                if (ValidateUser(model.Email, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.Email, false);
                    return RedirectToAction("Index", "Teacher");
                }
                else
                {
                    ViewBag.ErrorMessage = "Invalid email or password.";
                }
            }

            return View(model);
        }

        private bool ValidateUser(string email, string password)
        {
            // 验证用户凭据的代码
            if (email == "123456@outlook.com")
            {
                if(!string.IsNullOrEmpty(password))
                {
                    if (password == "1234567")
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        [HttpGet]
        public ActionResult Check()
        {
            ViewBag.Message = "输入学号查询成绩";
            return View();
        }
        // GET: Student
        [HttpPost]
        public ActionResult Check(GradeModel stu)
        {
            GradeModel tGrade = new GradeModel();
            var temp1 = DBHelper.DB.成绩表.Where(o => o.学号.Equals(stu.成绩.学号)).FirstOrDefault();
            if (temp1 == null)
            {
                ViewBag.ErrorMessage = $"未找到学号为{stu.成绩.学号}的学生";
            }
            var temp2 = DBHelper.DB.习题表.Where(o => o.学号.Equals(stu.成绩.学号)).FirstOrDefault();
            var temp3 = DBHelper.DB.测试表.Where(o => o.学号.Equals(stu.成绩.学号)).FirstOrDefault();
            tGrade.成绩 = temp1;
            tGrade.习题 = temp2;
            tGrade.测试 = temp3;

            return View(tGrade);
        }
    }
}